<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Paginator;
use think\Cache;
use think\DB;

class Searchinfo extends BaseController
{
	/**
	 * 借出书籍
	 */
	public function searchinfo($bname){
        //如果没有登录过跳转到登录页
        $this->checkIndex();
		$uname = Session::get('userinfo');
        $this->assign('uname',$uname);
		$this->assign('bname',$bname);
		return view();
	}

	public function pagesearch($bname){
		$page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }
		$userid = Session::get('userid'); 
        //搜索出的馆内书籍
        $booked = db('books')->where('isdelete',1)->where('bname','like','%'.$bname.'%')->where('bstates',1)->select();
        //书籍是否已被本人申请
        $borrowstates = db('borrow')->where('states',3)->where('uid',$userid)->select();
        // dump($borrowstates);die;
        foreach($booked as $c => $d){
            $booked[$c]['apply'] = 0;
            foreach($borrowstates as $a => $b){
                if($b['bid']==$d['id']){
                    $booked[$c]['apply'] = 1;
                }
            }
        }
        //借出书籍
        $book = db('books')->where('isdelete',1)->where('bname','like','%'.$bname.'%')->where('bstates',2)->select();
        //借出图书
        $bookout = array();
        $m = 0;
        foreach ($book as $key => $value) {
            if($value['bstates']==2){
                $bookout[$m]['bid'] = $book[$key]['id'];
                $bookout[$m]['img'] = $book[$key]['img'];
                $bookout[$m]['bname'] = $book[$key]['bname'];
                $bookout[$m]['type'] = 0;
                //书籍是否在本人手中 0-不在，1-在
                $bookout[$m]['power'] = 0;
                $borrow = db('borrow')->where('bid',$book[$key]['id'])->select();
                foreach($borrow as $k => $val){
                    if($val['uid']==$userid){
                        if($val['states']==8){
                            //书籍是否已被本人预约
                            $bookout[$m]['type'] = 1;
                        }else if($val['states']==1){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==5){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==6){
                            $bookout[$m]['power'] = 1;
                            $bookout[$m]['states'] = $val['states'];
                        }
                    }
                    
                    //此时时间戳
                    $now = time();
                    if($val['states']==1|$val['states']==5|$val['states']==6){
                        $uinfo = db('user')->where('id',$val['uid'])->find();
                        $bookout[$m]['uname'] = $uinfo['name'];
                        $bookout[$m]['booktime'] = date('Y/m/d', $val['booktime']);
                        $bookout[$m]['expectedReturnTime'] = date('Y/m/d', $val['expectedReturnTime']);
                        if($val['expectedReturnTime']<$now){
                            $overtime = (int)ceil(($now-$val['expectedReturnTime'])/3600/24);
                            $bookout[$m]['overtime'] = $overtime;
                        }else{
                            $bookout[$m]['overtime'] = 0;
                        }
                    }
                }
                $m++;
            }
        }
        $data = array_merge($booked,$bookout);
        $query = Publicmethod::pageing($data,$page);
        if($query){
            return $query;
        }
        return 0;
	}

}